#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "pthread.h"

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "CaseBaseUtils.h"


#define N_PROBLEMS	9

char *assoc[9][2]={ {"trains-description","trains-problem"},
					{"chemical-compound","toxic-problem"},
					{"sponge","sponge-problem"},
					{"soybean","soybean-problem"},
					{"robot-description","robot-problem"},
					{"monks-description","monks-problem"},
					{"person","uncle-problem"},
					{"artificial-data","artificial-data-problem"},
					{"animal","zoo-problem"}};


Sort *get_case_sort(Ontology *o,Sort *problem_sort)
{
	int i;

	for(i=0;i<N_PROBLEMS;i++) {
		if (strcmp(problem_sort->get(),assoc[i][0])==0) return o->get_sort(assoc[i][1]);
	} /* if */ 

	return 0;
} /* get_case_sort */ 


Sort *get_problem_sort(Ontology *o,Sort *case_sort)
{
	int i;

	for(i=0;i<N_PROBLEMS;i++) {
		if (strcmp(case_sort->get(),assoc[i][1])==0) return o->get_sort(assoc[i][0]);
	} /* if */ 

	return 0;
} /* get_problem_sort */ 


FeatureTerm *get_description(FeatureTerm *c)
{
	return c->featureValue("description");
} /* get_description */ 


FeatureTerm *get_solution(FeatureTerm *c)
{
	FeatureTerm *f;

	f=c->featureValue("solution");

	if (strcmp(c->getSort()->get(),"sponge-problem")==0) {
		FeatureTerm *f2;
		f2=f->featureValue("order");
		delete f;
		f=f2;
	} /* if */ 
	return f;
} /* get_solution */ 


FeatureTerm *build_case(FeatureTerm *p,FeatureTerm *s,Ontology *o)
{
	FeatureTerm *c=0;
	Sort *case_sort=0;
	char *assoc[3][2]={{"sponge","sponge-problem"},
					   {"soybean","soybean-problem"},
					   {"animal","zoo-problem"}};

	case_sort=get_case_sort(o,p->getSort());	
	if (case_sort==0) return 0;

	if (p->getName()!=0) {
		char *name_tmp;
		name_tmp=new char[strlen(p->getName()->get())+1+5];
		sprintf(name_tmp,"case-%s",p->getName()->get());
		c=new FeatureTerm(name_tmp,case_sort,0);
		delete []name_tmp;
		c->defineFeatureValue("description",new FeatureTerm(p));
	} else {
		c=new FeatureTerm((Symbol *)0,case_sort,0);
		c->defineFeatureValue("description",new FeatureTerm(p));
	} /* if */ 

	if (strcmp(p->getSort()->get(),"sponge")==0) {
		FeatureTerm *tmp;

		tmp=new FeatureTerm((Symbol *)0,o->get_sort("sponge-solution"),0);
		tmp->defineFeatureValue("order",new FeatureTerm(s));
		c->defineFeatureValue("solution",tmp);
	} else {
		c->defineFeatureValue("solution",new FeatureTerm(s));
	} /* if */ 

	return c;
} /* build_case */ 


bool is_a_problem(FeatureTerm *p)
{
	return is_a_problem(p->getSort());
} /* is_a_problem */ 


bool is_a_case(FeatureTerm *p)
{
	return is_a_case(p->getSort());
} /* is_a_case */ 



bool is_a_problem(Sort *s)
{
	if (strcmp(s->get(),"trains-description")==0) return true;
	if (strcmp(s->get(),"chemical-compound")==0) return true;
	if (strcmp(s->get(),"sponge")==0) return true;
	if (strcmp(s->get(),"soybean")==0) return true;
	if (strcmp(s->get(),"robot-description")==0) return true;
	if (strcmp(s->get(),"monks-description")==0) return true;
	if (strcmp(s->get(),"person")==0) return true;
	if (strcmp(s->get(),"artificial-data")==0) return true;
	if (strcmp(s->get(),"animal")==0) return true;
	
	return false;
} /* is_a_problem */ 


bool is_a_case(Sort *s)
{
	if (strcmp(s->get(),"trains-problem")==0) return true;
	if (strcmp(s->get(),"toxic-problem")==0) return true;
	if (strcmp(s->get(),"sponge-problem")==0) return true;
	if (strcmp(s->get(),"soybean-problem")==0) return true;
	if (strcmp(s->get(),"robot-problem")==0) return true;
	if (strcmp(s->get(),"monks-problem")==0) return true;
	if (strcmp(s->get(),"uncle-problem")==0) return true;
	if (strcmp(s->get(),"artificial-data-problem")==0) return true;
	if (strcmp(s->get(),"zoo-problem")==0) return true;

	return false;
} /* is_a_case */ 